package com.fram.controller.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.api.project.request.CategoryRequest;
import com.fram.bean.api.project.request.OrderCommentRequest;
import com.fram.bean.api.project.request.ProjectGoodsSearchRequest;
import com.fram.bean.api.project.response.ProjectGoodsSearchResponse;
import com.fram.bean.sys.PageBean;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseController;
import com.fram.service.api.v1.ProjectService;
import com.github.pagehelper.PageInfo;

/**
 * @作者：大晓刚 @时间 2020年5月19日 @dascription: ->前台 商品展示 （查询）
 */
@RestController
@RequestMapping("/api/{version}/project")
public class ProjectController extends BaseController {
	// 日志
	private static Logger LOG = LoggerFactory.getLogger(ProjectController.class);

	@Autowired
	private ProjectService projectService;

	/**
	 * 根据类别 展示 商品
	 * @return
	 * @throws ProException
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/category", method = RequestMethod.POST)
	private ResponseMsg getCategoryById(@RequestBody CategoryRequest request) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(projectService.selectProductCategoryList(request));
		} catch (ProException e) {
			LOG.error("-产品-店铺类别 商品分类 展示出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-产品-店铺类别 商品分类 展示异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/**
	 * 商品展示 /**商品名称private String name;
	 * 
	 * @param bean
	 * @return
	 * @throws ProException
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/list")
	private ResponseMsg getProductExhibition(@RequestBody PageBean<ProjectGoodsSearchRequest> bean) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			try {
				bean.getParam().setUserId(getApiUserId());
			} catch (Exception e) {
			}
			PageInfo<ProjectGoodsSearchResponse> productresponse = projectService.queryProjectGoodsList(bean);
			msg.setData(productresponse);
		} catch (ProException e) {
			LOG.error("-产品-分类展示根据某店铺展示分类--列表获取出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-产品-分类展示根据某店铺展示分类--列表获取异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	// 商品详情
	@ApiVersion(1.0)
	@RequestMapping(value = "/detail")
	private ResponseMsg selectGoodsById(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			Long userId = null;
			try {
				userId = getApiUserId();
			} catch (Exception e) {
			}
			msg.setData(projectService.projectGoodsDetail(id, userId));
		} catch (ProException e) {
			LOG.error("-产品-商品详情点击商品出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-产品-商品详情点击商品异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "/comment/list")
	private ResponseMsg projectComment(@RequestBody PageBean<OrderCommentRequest> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(projectService.projectComment(request));
		} catch (ProException e) {
			LOG.error("-产品-商品评论查询出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-产品-商品评论查询异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "/auth/update/status")
	private ResponseMsg updatestatus(Long goodsId, Integer status) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			projectService.projectStatus(getApiUserId(), goodsId, status);
		} catch (ProException e) {
			LOG.error("-产品-产品上下架出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-产品-产品上下架异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

}
